<template>
  <m-window-frame
    title="校验分组"
    style="height: 100%"
    class="v-validate-group"
    okText=""
    cancelText="关闭"
    @cancel="handleCancel"
  >
    <div class="_content g-fd-c">
      <MAttrTips type="validateGroup" />
      <div class="g-m-24 g-flex g-flex-1 g-ov-hidden">
        <div class="g-flex-0 g-fd-c g-ov-hidden _left-area">
          <div class="_header">
            <span>{{ $t("校验分组") }}</span>
            <div
              v-if="isEdit"
              class="g-ai-c _header_right"
            >
              <MDropdown>
                <Tooltip
                  :content="$t('新增')"
                  placement="bottom"
                  :show-after="500"
                >
                  <img
                    class="_header_right_btn"
                    src="statics/icons/app/ruleGroupAdd.svg"
                  />
                </Tooltip>
                <template #dropdown>
                  <MDropdownMenuItem @click="model.handleClickAdd(StType['Customization::ValidationSuite'])">
                    {{ $t("不自动校验分组") }}
                  </MDropdownMenuItem>
                  <MDropdownMenuItem @click="model.handleClickAdd(StType['Customization::ActivityValidationSuite'])">
                    {{ $t("自动校验分组") }}
                  </MDropdownMenuItem>
                </template>
              </MDropdown>
              <Tooltip
                :content="$t('删除')"
                placement="bottom"
                :show-after="500"
              >
                <img
                  src="statics/icons/app/ruleGroupRemove.svg"
                  class="g-m-l-10 _header_right_btn"
                  :style="{
                    opacity: !model.selectedGroup?.isInteractive ? 0.5 : 1,
                    cursor: !model.selectedGroup?.isInteractive ? 'not-allowed' : 'pointer'
                  }"
                  @click="model.handleRemoveGroup"
                />
              </Tooltip>
            </div>
          </div>
          <div class="g-flex-1 g-fd-c __left-content">
            <div class="group-content-border">
              <p
                v-for="item in model.groupList"
                :key="item.instanceId"
                class="group-field"
                @click="model.handleClickGroup(item)"
              >
                <img
                  :src="item.icon"
                  class="_tree-node-icon"
                />
                <span
                  v-if="!item.isEdit"
                  class="text"
                  :class="{
                    _selected: model.selectedGroup?.instanceId === item.instanceId
                  }"
                  :style="!item.isInteractive ? 'color: #bfbfbf;' : ''"
                  @dblclick="!item.isInteractive || !isEdit ? null : handleDblClick(item)"
                >
                  {{ item.instanceName }} 【{{ item.qualifiedName }}】
                </span>
                <el-input
                  v-if="item.isEdit"
                  :ref="el => setInputRef(el, item.instanceId)"
                  v-model="model.editingName"
                  size="small"
                  class="node-input"
                  style="width: 300px"
                  :disabled="!model.selectedGroup?.isInteractive"
                  @blur="model.saveName(item, model.editingName)"
                  @keyup.enter="model.saveName(item, model.editingName)"
                  @keyup.esc="model.cancelEdit(item)"
                />
              </p>
            </div>
          </div>
        </div>
        <div class="g-flex-1 _right-area g-fd-c g-ov-hidden">
          <div class="_header">{{ $t("校验规则") }}</div>
          <div class="g-flex-1 g-fd-c __right-content">
            <div class="g-flex-1 g-ov-auto __right-content_tree">
              <el-tree
                ref="treeRef"
                :data="model.treeData"
                node-key="instanceId"
                :default-expanded-keys="model.expandedKeys"
                :expand-on-click-node="false"
              >
                <template #default="{ node, data }">
                  <span class="custom-tree-node">
                    <span
                      class="tree_node"
                      :class="{ _selected: model.selectedNode?.instanceId === data.instanceId && model.selectedNode?.ownerId }"
                      @click.stop="model.handleNodeClick(data)"
                    >
                      <span class="g-ai-c v-tree-text">
                        <img
                          :src="data.icon"
                          class="_tree-node-icon"
                        />
                        <span
                          class="text"
                          :title="data.instanceName"
                          style="flex: 1; overflow: hidden; text-overflow: ellipsis"
                        >
                          {{ data.instanceName }}
                        </span>
                      </span>
                      <img
                        src="statics/icons/app/attrRule.svg"
                        style="flex-shrink: 0; opacity: 0; transition: opacity 0.2s"
                        class="hover-icon"
                        @click.stop="model.handleShowAttr(data)"
                      />
                    </span>
                  </span>
                </template>
              </el-tree>
            </div>
            <div
              v-if="isEdit"
              class="__right-content_bottom g-flex-0"
            >
              <el-button @click="() => model.handleRule()">
                <img src="statics/icons/app/ruleSetting.svg" />
                {{ $t("规则管理") }}
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </m-window-frame>
</template>
<script lang="ts" setup>
import { reactive, ref, nextTick, onMounted } from "vue";
import { MWindowFrame, MDropdown, MDropdownMenuItem, AttrTips as MAttrTips, ipcWindowUtil, StType } from "@/mdesign-lib/mdesign-app.umd.js";
import { WindowName } from "@/mdesign-lib/mdesign-main.js";
const query = app.$route.query;
import { ValidateGroupModel, ValidationModelInstanceVo } from "./ValidateGroupModel";
import { Tooltip } from "@/mdesign-lib/mdesign-graph.umd.js";

const model = reactive(new ValidateGroupModel(query.projectId as string));

const inputRefs = ref<Record<string, any>>({});
const setInputRef = (el: any, id: string) => {
  if (el) {
    inputRefs.value[id] = el;
  }
};

onMounted(() => {
  model.init();
});
const isEdit = query.isEdit === "true";

function handleDblClick(item: ValidationModelInstanceVo) {
  model.editingName = item.instanceName;
  item.isEdit = true;

  nextTick(() => {
    inputRefs.value[item.instanceId]?.focus();
  });
}

function handleCancel() {
  ipcWindowUtil.closeWindow({ windowName: WindowName.ValidateGroupWindow, success: false });
}
</script>

<style lang="scss">
@import "@/assets/css/theme.scss";
.v-validate-group {
  ._content {
    height: 100%;
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;
      width: 100%;
      overflow-x: hidden;
      .tree_node {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        .v-tree-text {
          width: calc(100% - 20px);
        }
        .text {
          padding: 0 5px;
        }
      }
    }
    ._header {
      padding-left: 12px;
      font-weight: bold;
      line-height: 26px;
      height: 28px;
      background: $toolbar-bg-color;
      border: 1px solid #d9d9d9;
      display: flex;
      align-items: center;
      justify-content: space-between;
      ._header_right {
        margin-right: 10px;
        align-items: center;
        line-height: 10px;
        ._header_right_btn:hover {
          cursor: pointer;
        }
      }
    }
    ._left-area {
      width: 50%;
      border-bottom: 1px solid #d9d9d9;
      display: flex;
      flex-direction: column;
      ._header {
        flex-shrink: 0;
      }
      .__left-content {
        flex: 1;
        padding: 0;
        border-left: 1px solid #d9d9d9;
        border-bottom: 1px solid #d9d9d9;
        overflow: auto;
        .group-content-border {
          height: auto;
          padding: 10px;
        }
        .group-field {
          display: flex;
          align-items: center;
          line-height: 24px;
          white-space: nowrap;
          .text {
            padding: 0 2px;
          }
        }
      }
      ._selected {
        background-color: $theme-color;
        color: #fff;
      }
    }
    ._tree-node-icon {
      position: relative;
      width: 16px;
      height: 16px;
    }
    ._right-area {
      ._placeholder-img {
        width: 120px;
        height: 120px;
      }
      //._selected {
      //  .text {
      //    background-color: $theme-color;
      //    color: #fff;
      //  }
      //}
      .__right-content {
        border-right: 1px solid #d9d9d9;
        border-left: 1px solid #d9d9d9;
        border-bottom: 1px solid #d9d9d9;
        display: flex;
        flex: 1;
        overflow: hidden;
        .__right-content_tree {
          padding: 10px;
        }
        .__right-content_bottom {
          border-top: 1px solid #d9d9d9;
          padding: 15px;
          height: 60px;
          display: flex;
          justify-content: right;
        }
      }

      ._btn-group {
        border-top: 1px solid #d9d9d9;
        padding: 8px;
      }
      .tree_node:hover .hover-icon {
        opacity: 1 !important;
      }
    }
  }
}
</style>
